﻿Public Class vueIndicateurs_V2
    Inherits vueCompte_V2

    Public Overrides WriteOnly Property moteurTxGaranti2() As moteurTauxGaranti2.Moteur_Methode2
        Set(ByVal value As moteurTauxGaranti2.Moteur_Methode2)
            MyBase.moteurTxGaranti2 = value
            AddHandler _resultatsScenarioCentral.compteTresorerie_hasChanged, AddressOf onResultatsScenCent_CompteTreso_HasChanged
            AddHandler _resultatsScenarioCentral.compteIndicateur_hasChanged, AddressOf onResultatsScenCent_CompteIndicateurs_HasChanged
        End Set
    End Property

#Region "Initialisation"
    Protected Overrides Sub initNbLignes()
        If Me.ColumnCount = 0 Then
            Me.ColumnCount = 1
        End If
        For i As Integer = 1 To 30
            Me.Rows.Add()
            Me.Rows(i - 1).Height = 15
        Next
        'Cas particuliers
        Rows(3).Height = 6
        Rows(9).Height = 6
        Rows(14).Height = 6
    End Sub
    Protected Overrides Sub initTitresBlocs()
        initLignesBlocCompteTresorerie()
        initLignesBlocROE()
        initLignesBlocSP()
    End Sub
    Protected Overrides Sub initLignesTitresGeneraux()
        Me.Rows(0).Cells(0).Value = ""
    End Sub
    Private Sub initLignesBlocCompteTresorerie()
        'Titre du compte de résultat
        Me.Rows(2).Cells(0).Value = "COMPTE DE TRESORERIE"
        'Titre du compte de trésorerie
        Me.Rows(4).Cells(0).Value = "Variation des capitaux sociaux dûrs "
        Me.Rows(5).Cells(0).Value = "Résultat distribué (perte si nég.)"
        Me.Rows(6).Cells(0).Value = "Flux actionnaire"
    End Sub
    Private Sub initLignesBlocROE()
        'Titre du compte de résultat
        Me.Rows(8).Cells(0).Value = "ROE"
        'Titre du compte de trésorerie
        Me.Rows(10).Cells(0).Value = "Résultat net / Fds Propres Ouv."
        Me.Rows(11).Cells(0).Value = "Résultat net / Capitaux Alloués"
    End Sub
    Private Sub initLignesBlocSP()
        Me.Rows(13).Cells(0).Value = "S/P"
        'Titre du compte de trésorerie
        Me.Rows(15).Cells(0).Value = "DC - (Sinistres réglés / Primes Techniques)"
        Me.Rows(16).Cells(0).Value = "INC/INV - (Sinistres réglés / Primes Techniques)"
        Me.Rows(17).Cells(0).Value = "TOTAL - (Sinistres réglés / Primes Techniques)"
    End Sub
#End Region

#Region "Méthodes de dessin"
    Protected Overrides Sub paintBlocs(ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
        'Compte de trésorerie
        paintBloc(e, 2, 6)
        'Bloc ROE
        paintBloc(e, 8, 11)
        'Bloc S/P
        paintBloc(e, 13, 17)
    End Sub
    Protected Overrides Function cellFormat(ByVal rowIndex As String, ByVal columnIndex As String) As String
        If columnIndex <> 0 Then
            If rowIndex = 10 Or rowIndex = 11 Or rowIndex = 15 Or rowIndex = 16 Or rowIndex = 17 Then
                Return _formatDefaut & "%"
            ElseIf rowIndex <> 0 Then
                Return _formatDefaut
            Else
                Return ""
            End If
        Else
            Return ""
        End If
    End Function
    Protected Overrides Function cellBackColor(ByVal rowIndex As String, _
                                   ByVal columnIndex As String, _
                                   ByVal ligneHautBloc As Integer, _
                                   ByVal ligneBasBloc As Integer) As System.Drawing.Color
        If rowIndex = ligneHautBloc Then
            Return Color.FromArgb(231, 213, 231)
        ElseIf rowIndex = 6 Or rowIndex = 17 Then
            Return Color.FromArgb(255, 230, 30)
        Else
            Return Color.White
        End If
    End Function
    Protected Overrides Function cellAlignment(ByVal rowIndex As Integer, ByVal columnIndex As Integer) As StringAlignment
        If columnIndex = 0 Then Return StringAlignment.Near Else Return StringAlignment.Far
    End Function
    Protected Overrides Function cellFont(ByVal rowIndex As Integer, ByVal columnIndex As Integer, ByVal ligneHautBloc As Integer, ByVal ligneBasBloc As Integer) As Font
        If rowIndex = ligneHautBloc Or rowIndex = 6 _
            Or ((rowIndex = 10 Or rowIndex = 11) And columnIndex <> 0) _
            Or rowIndex = 17 Then
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Bold)
        Else
            Return New System.Drawing.Font("Tahoma", 9, FontStyle.Regular)
        End If
    End Function
#End Region

#Region "Rafraichissements des chiffres des comptes"
    Private Sub onResultatsScenCent_CompteTreso_HasChanged(ByVal sender As Object, ByVal e As EventArgs)
        refreshValeursCompteTreso()
    End Sub
    Private Sub onResultatsScenCent_CompteIndicateurs_HasChanged(ByVal sender As Object, ByVal e As EventArgs)
        refreshValeursIndicateurs()
    End Sub
    Protected Overrides Sub refreshValeursBlocs()
        refreshValeursCompteTreso()
        refreshValeursIndicateurs()
    End Sub
    Protected Overrides Sub refreshTitresGeneraux()
        'Années de projection
        For j As Integer = 1 To _nbAnneesProjection
            Me.Rows(0).Cells(j).Value = CStr(j) - 1 + _anneeDepart
        Next
    End Sub
    Protected Sub refreshValeursCompteTreso()
        If Not _resultatsScenarioCentral Is Nothing Then
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            If Not _resultatsScenarioCentral.compteTresorerie Is Nothing Then
                With _resultatsScenarioCentral.compteTresorerie
                    If Not .variationCapiSocial Is Nothing Then
                        For i As Integer = 1 To Math.Min(_nbAnneesProjection, .variationCapiSocial.Count)
                            If Not .variationCapiSocial Is Nothing Then Me.Rows(4).Cells(i).Value = -.variationCapiSocial(i - 1) / unite
                            If Not .resultatDistribue Is Nothing Then Me.Rows(5).Cells(i).Value = .resultatDistribue(i - 1) / unite
                            If Not .fluxActionnaire Is Nothing Then Me.Rows(6).Cells(i).Value = .fluxActionnaire(i - 1) / unite
                        Next
                    End If
                End With
                Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
            End If
        End If
    End Sub
    Public Sub refreshValeursIndicateurs()
        If Not _resultatsScenarioCentral Is Nothing Then
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
            If Not _resultatsScenarioCentral.compteIndicateur Is Nothing Then
                With _resultatsScenarioCentral.compteIndicateur
                    If Not .ROE_resNetsFdsPropres Is Nothing Then
                        For i As Integer = 1 To Math.Min(_nbAnneesProjection, .ROE_resNetsFdsPropres.Count)
                            Me.Rows(10).Cells(i).Value = .ROE_resNetsFdsPropres(i - 1)
                            Me.Rows(11).Cells(i).Value = .ROE_resNetsCapiAlloues(i - 1)
                            Me.Rows(15).Cells(i).Value = .SP_techDC(i - 1)
                            Me.Rows(16).Cells(i).Value = .SP_techINCINV(i - 1)
                            Me.Rows(17).Cells(i).Value = .SP_techTot(i - 1)
                        Next
                    End If
                End With
            End If
            Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        End If
    End Sub
#End Region

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            RemoveHandler _resultatsScenarioCentral.compteTresorerie_hasChanged, AddressOf onResultatsScenCent_CompteTreso_HasChanged
            RemoveHandler _resultatsScenarioCentral.compteIndicateur_hasChanged, AddressOf onResultatsScenCent_CompteIndicateurs_HasChanged
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

End Class